#!/usr/bin/python
# -*- coding: UTF-8 -*-
#cron:0 6 6 ? * 3,6 
"""
File: xiwoxueyuanpingluntoupiao.py
Author: shuaiqidelinlaoshi
cron: 0 6 6 ? * 3,6 
new Env('希沃学院投票评论任务测试');
Update: 2024/11/22
"""

import requests
import base64
import json
import time
import notify2

t = time.time()
timestamp = int(round(t * 1000))

import os
import sys
import random


# 定义两个账号信息，实际应用中可根据需要扩展账号数量
# 从环境变量中获取账号密码信息
env_accounts_info = os.environ.get('xiwo')

# 解析环境变量中的账号密码信息，存储为账号字典列表
accounts = []
if env_accounts_info:
    account_pairs = env_accounts_info.split('@')
    for pair in account_pairs:
        username, password = pair.split('&')
        accounts.append({'username': username, 'password': password})
else:
    print("环境变量中未获取到账号密码信息，请检查设置！")

# 根据账号获取token
def token(account):
    headers = {
        'Cookie': 'x-auth-app=EasiNote5; x-auth-token=; acw_tc=1a0c595817308041547308041547398024e00e398f9ff3f4cdf743e11908bdb62cff81d',
        'X-APM-TraceId': '6a7e4cf4c71645dfb92c37d12f17709',
        'Content-Type': 'application/json; charset=utf-8',
        'Content-Length': '109'}
    payload1 = base64.b64decode(account['password'])
    html = requests.request("POST", "https://edu.seewo.com/api/v1/auth/login", headers=headers, data=payload1)
    a = json.loads(html.text)
    token = a["data"]["token"]
    print(f'获取账号 {account["username"]} 的最新token：{token}')
    return token

# 获取本周主题的uid,及投票的uid
def uid(token):
    headers = {
     'sec-ch-ua':'"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
     'accept':'application/json, text/plain, */*',
     'x-csrf-token':'3z-f9k2czzJSCuT7ssY5zSBE',
     'sec-ch-ua-mobile':'?1',
     'x-kish-token-key':'token',
     'user-agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Mobile Safari/537.36',
     'sec-ch-ua-platform':'"Android"',
     'sec-fetch-site':'same-origin',
     'sec-fetch-mode':'cors',
     'sec-fetch-dest':'empty',
     'referer':'https://m.seewoedu.cn/college/topic/square',
     'accept-encoding':'gzip, deflate, br',
     'accept-language':'zh-CN,zh;q=0.9',
     'cookie':f'prfb=0bZkD9FL2YHaxRA5JgIo0w/+Bnq9gc9uS/weQEcjkLYcOEZ1AJsG/I4tDCW1wGB3; prfb.sig=MzxvEP5V92_nnm75oVsNTpsOonJHwTB2uDUupu7t9bw; prfbe=1; prfbe.sig=EkUBoLJrN8CglDbsu17Kvk7W2APZDlVAQbDmjBSMFGw; isLogin.sig=6cEa4OH0TcIMNn5kN4nya3RJcZZkb-5Q5Afzqx1MZns; userId=5f20ea0cfbff401dbb7119eb7cf7d530; stageName=%E5%B0%8F%E5%AD%A6; stageId=CodeSchoolStage_0; subjectName=%E4%BF%A1%E6%81%AF%E7%A7%91%E6%8A%80; subjectId=CodeCourseSubject_16; sw_sid=user_geCG_aYkN9WJMpk63Jz1Uj_Euo73QwLF; token={token}; token.sig=QjaA4nJkCwqfIZ6ESvLrFv4o7gXBvFWMLHp9BPLeY8E; isLogin=true; csrfToken=3z-f9k2czzJSCuT7ssY5zSBE; Hm_lvt_28e60218260284b8df3efca2fc9f65ad=1731577081,1731650375,1731937504,1731974651; HMACCOUNT=3323316B7FE79462; ab_uid=5f20ea0cfbff401dbb7119eb7cf7d530; channel=; acw_tc=ac11000117320250952588887e0043e162317d056150b54707126a3c741ff4; Hm_lpvt_28e60218260284b8df3efca2fc9f65ad=1732026228'}
    payload=None

    response0 = requests.request("GET", f"https://m.seewoedu.cn/apiv3/v1/topic/last?time={timestamp}-yW4dLDpI_KIa6R7RKlPDY", headers=headers, data=payload)
    topicuid = response0.json()["data"]["topicUid"]
    topicContent = response0.json()["data"]["topicContent"]
    optionUid1 = response0.json()["data"]["topicOption"][0]["optionUid"]
    optionContent1 = response0.json()["data"]["topicOption"][0]["optionContent"]
    optionUid2 = response0.json()["data"]["topicOption"][1]["optionUid"]
    optionContent2 = response0.json()["data"]["topicOption"][1]["optionContent"]
    #print(topicuid,topicContent,optionUid1,optionContent1,optionUid2,optionContent2)
    return topicuid,topicContent,optionUid1,optionContent1,optionUid2,optionContent2

#投票
def toupiao(token):
    import requests

    headers = {
        'content-length': '0',
        'sec-ch-ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
        'accept': 'application/json, text/plain, */*',
        'x-csrf-token': '3z-f9k2czzJSCuT7ssY5zSBE',
        'sec-ch-ua-mobile': '?1',
        'x-kish-token-key': 'token',
        'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Mobile Safari/537.36',
        'sec-ch-ua-platform': '"Android"',
        'origin': 'https://m.seewoedu.cn',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://m.seewoedu.cn/college/topic/square',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9',
        'cookie': f'prfb=0bZkD9FL2YHaxRA5JgIo0w/+Bnq9gc9uS/weQEcjkLYcOEZ1AJsG/I4tDCW1wGB3; prfb.sig=MzxvEP5V92_nnm75oVsNTpsOonJHwTB2uDUupu7t9bw; prfbe=1; prfbe.sig=EkUBoLJrN8CglDbsu17Kvk7W2APZDlVAQbDmjBSMFGw; isLogin.sig=6cEa4OH0TcIMNn5kN4nya3RJcZZkb-5Q5Afzqx1MZns; userId=5f20ea0cfbff401dbb7119eb7cf7d530; stageName=%E5%B0%8F%E5%AD%A6; stageId=CodeSchoolStage_0; subjectName=%E4%BF%A1%E6%81%AF%E7%A7%91%E6%8A%80; subjectId=CodeCourseSubject_16; sw_sid=user_geCG_aYkN9WJMpk63Jz1Uj_Euo73QwLF; token={token}; token.sig=QjaA4nJkCwqfIZ6ESvLrFv4o7gXBvFWMLHp9BPLeY8E; isLogin=true; csrfToken=3z-f9k2czzJSCuT7ssY5zSBE; Hm_lvt_28e60218260284b8df3efca2fc9f65ad=1731577081,1731650375,1731937504,1731974651; HMACCOUNT=3323316B7FE79462; ab_uid=5f20ea0cfbff401dbb7119eb7cf7d530; channel=; acw_tc=ac11000117320250952588887e0043e162317d056150b54707126a3c741ff4; Hm_lpvt_28e60218260284b8df3efca2fc9f65ad=1732026228'}
    payload = None
    optionUid = random.choice([uid(token)[2], uid(token)[4]])
    msg = ''
    msg += f'本次主题uid：{uid(token)[0]}\n'
    msg += f'本次主题名称：{uid(token)[1]}\n'
    if optionUid == uid(token)[2]:
        msg +=f'本次投票选择：{uid(token)[3]}\n'
    else:
        msg += f'本次投票选择：{uid(token)[5]}\n'
    response0 = requests.request("PUT",f"https://m.seewoedu.cn/apiv3/v1/topic/{uid(token)[0]}/option/{optionUid}?time={timestamp}-tKxG6s5drzAQppg4n7zep",headers=headers, data=payload)
    msg += f'投票结果：{response0.json()["message"]}\n'
    return msg

#发表评论
def pinglun(token):
    import requests
    import base64

    headers = {
        'content-length': '45',
        'sec-ch-ua-platform': '"Android"',
        'x-csrf-token': '68SLf9bd1QXdadvydnPg5ICQ',
        'x-kish-token-key': 'token',
        'sec-ch-ua': '"Microsoft Edge";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
        'sec-ch-ua-mobile': '?1',
        'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36 Edg/131.0.0.0',
        'accept': 'application/json, text/plain, */*',
        'content-type': 'application/json',
        'origin': 'https://m.seewoedu.cn',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://m.seewoedu.cn/college/topic/publish/edit',
        'accept-encoding': 'gzip, deflate, br, zstd',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'priority': 'u=1, i',
        'cookie': f'acw_tc=ac11000117321885139867414e0030496de9d6c09503942845d4261e3fa1b1; csrfToken=68SLf9bd1QXdadvydnPg5ICQ; Hm_lvt_28e60218260284b8df3efca2fc9f65ad=1732188474; HMACCOUNT=42209AB7FA95880B; prfb=0Vw8EI77pz6LEVIw8Kp/mi2mFgyBy/PnJwG5LcMcHQniKO6ASIOsrsi8KUDxz/Uu; prfb.sig=8T0qoDo0AF31FJsU8IKLt20pyX-7LPsBDxG5P_nOmNY; prfbe=1; prfbe.sig=YigtUW3dSaV9Z4-j9W4uoyH2ZwLPlLNJzZCP_Mnfdt4; token={token}; token.sig=s4z7Vs4MduK0M7z6dLwhzppOOIVPbEkySeaFaAWTWgE; isLogin=true; isLogin.sig=6cEa4OH0TcIMNn5kN4nya3RJcZZkb-5Q5Afzqx1MZns; userName=15779752720; userId=5f20ea0cfbff401dbb7119eb7cf7d530; stageName=%E5%B0%8F%E5%AD%A6; stageId=CodeSchoolStage_0; subjectName=%E4%BF%A1%E6%81%AF%E7%A7%91%E6%8A%80; subjectId=CodeCourseSubject_16; ab_uid=5f20ea0cfbff401dbb7119eb7cf7d530; Hm_lpvt_28e60218260284b8df3efca2fc9f65ad=1732188522'}
    payload = base64.b64decode("eyJjb21tZW50Q29udGVudCI6ImFiY2RlZmciLCJoYXNTZWNyZXQiOnRydWV9")

    response0 = requests.request("POST",f"https://m.seewoedu.cn/apiv3/v1/topic/{uid(token)[0]}/comments/add?time={timestamp}-xUumxRuTDFByZRrUZSFdi",headers=headers, data=payload)
    msg = f'评论结果：{response0.json()["message"]}'
    return msg
# 领取奖励
def lingqujiangli(token):
    headers2 = {
        'Host': 'm.seewoedu.cn',
        'Connection': 'keep-alive',
        'Content-Length': '0',
        'Accept': 'application/json,text/plain,*/*',
        'x-auth-token': '',
        'x-csrf-token': 'GJMwTaGLAyPB8Xx-bdIDe8Bj',
        'traceparent': '00-4ed7c6024da2657b19effb7909462251-fb171e3139a2be3d-01',
        'User-Agent': 'Mozilla/5.0(Linux;Android6.0;Nexus5Build/MRA58N)AppleWebKit/537.36(KHTML,likeGecko)Chrome/86.0.4240.198MobileSafari/537.36',
        'Origin': 'https://m.seewoedu.cn',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Dest': 'empty',
        'Referer': 'https://m.seewoedu.cn/college/exchange/home',
        'Accept-Encoding': 'gzip,deflate,br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'Cookie': f'csrfToken=GJMwTaGLAyPB8Xx-bdIDe8Bj;Hm_lvt_28e60218260284b8df3efca2fc9f65ad=1657093789;prfb=ERoxU7M96nUxopq7l+3qiv/BqJF/Hq9VUa2ewqB9CXFMYz0fEdjzkkGuzKP6p3kP;prfb.sig=MGgEKXzUnKg2pfIMFJ0satNK0jpFupmJgdpyWeKLPFs;prfbe=1;prfbe.sig=EkUBoLJrN8CglDbsu17Kvk7W2APZDlVAQbDmjBSMFGw;isLogin.sig=6cEa4OH0TcIMNn5kN4nya3RJcZZkb-5Q5Afzqx1MZns;userName=15779752720;userId=5f20ea0cfbff401dbb7119eb7cf7d530;stageName=%E5%B0%8F%E5%AD%A6;stageId=CodeSchoolStage_0;subjectName=%E4%BF%A1%E6%81%AF%E6%8A%80%E6%9C%AF;subjectId=CodeCourseSubject_16;ab_uid=5f20ea0cfbff401dbb7119eb7cf7d530;token={token};token.sig=GtcGSuqh0haCvY_Qw2Mwxz1oHZ_fnFYMVNTEq7vSrqI;isLogin=true;_csrf=5SAK8vDDpBHVmRM3ut_ZtHmx;sw_sid=user_t8sK1dY6BYFbiKqpqHhy7YNwkFVTYXIo;acw_tc=2f624a1c16571089399464268e66fb5812b97420ad608efbabfa6bf477e7f4;Hm_lpvt_28e60218260284b8df3efca2fc9f65ad=1657109110',

    }
    renwuid = ['24f4046983ba47a5a2091a4644f4ebe1','24f4046983ba47a5a2091a4644f4ebe9']
    msg = ''
    for i in renwuid:
        url = f'https://m.seewoedu.cn/apis/v1/tasks/{i}/integral?time={timestamp}-3sN0gvUM5AY'
        print(url)
        #time.sleep(random.random() * 50)
        res = requests.put(url=url, headers=headers2)
        msg += f'领取奖励：{res.json()["message"]}\n'
        print(f'1', res.text)
    return msg

if __name__ == "__main__":
    title = "希沃学院投票评论任务测试(多账号)"
    contents = []
    for account in accounts:
        token_value = token(account)
        b = f'--------🌸任务开始({account["username"]})🌸--------\n'
        a = '-'*50
        c = '\n'
        content = f'{b}{toupiao(token_value)}{pinglun(token_value)}{c}{lingqujiangli(token_value)}{c}'
        contents.append(content)
#print(contents)
contents_str = "".join(contents)
print(contents_str)
notify2.send(title,contents_str)
#lingqujiangli(token)